package com.jwong.test.operator;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.Operator;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class OperatorConfig {

    @Bean
    public WebPageReconciler webPageReconciler() {
        return new WebPageReconciler(kubernetesClient());
    }

    @Bean
    public KubernetesClient kubernetesClient() {
        Config config = new ConfigBuilder().withNamespace(null).build();
        return new DefaultKubernetesClient(config);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Operator operator(List<Reconciler> controllers) {
        Operator operator = new Operator(kubernetesClient(), DefaultConfigurationService.instance());
        controllers.forEach(operator::register);
        return operator;
    }

}
